//
//  RadioPageController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/16.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "RadioPageController.h"
#import "RadioModel.h"
#import "RadioCell.h"
#import "PWNetWorking.h"
#import "UIImageView+WebCache.h"

@interface RadioPageController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation RadioPageController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建tableView
    [self createTableView];
    
    // 数据
    [self dataHandler];
}
// 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor grayColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[RadioCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
// 数据
- (void)dataHandler
{
    NSString *str = @"http://api2.pianke.me/ting/radio";
    //auth=&client=1&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&version=3.0.6

    NSDictionary *bodyDic = @{@"auth": @"", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:str body:bodyDic completion:^(id result) {
        self.dataArr = [RadioModel modelHanderWithArray:result[@"data"][@"alllist"]];
        NSLog(@"%@", result);
        [_tableView reloadData];
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    RadioModel *m = _dataArr[indexPath.row];
    [cell.coverimg sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.titleLabel.text = m.title;
    cell.unameLabel.text = m.uname;
    cell.describeLabel.text = m.desc;
    cell.countLabel.text = [m.count stringValue];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
